package com.bilibili.juc.completablefuture;

import lombok.Data;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * allof的用法CompletableFuture.allOf记得写在可执行的代码块，
 * 还有先写CompletableFuture<Void>返回结果，不然获取不到方法
 */
public class CompletableFutureAll {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //下面用异步任务给它设值
        Student student = new Student();
        CompletableFuture<Void> task = CompletableFuture.runAsync(() -> {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    student.setName("蔡徐坤");
                }
        );
        CompletableFuture<Void> task2 = CompletableFuture.runAsync(() -> {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    student.setAge(24);
                }
        );
        CompletableFuture<Void> task3 = CompletableFuture.runAsync(() -> {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    student.setHeight(180.00);
                }
        );
        //阻塞获取结果,这里没有返回值为空，只是为了阻塞
        CompletableFuture.allOf(task,task2,task3).get();
        System.out.println(student.toString());
    }


}


/**
 * 学生资源类
 */
@Data
class Student {
    private int age;
    private String name;
    private double height;
}